package org.jiucai.app.demo.auth.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jiucai.appframework.base.util.IpUtil;
import org.jiucai.cas.auth.dao.AuthDao;
import org.jiucai.cas.auth.service.AuthResultServiceImpl;
import org.jiucai.cas.domain.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录事件服务类
 * @author zhaidw
 *
 */
@Service("authResultService")
public class AppAuthResultServiceImpl extends AuthResultServiceImpl {
	
	@Autowired
	protected AuthDao authDao;
	
	@Override
	public void onLoginSuccess(UserInfo user, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String loginIp = IpUtil.getIp(request);
		
		
		if( null != user){
			//密码不应该被保存到对象中
			user.setPassword("[Protected]");
			
			
			user.setLastLoginIp(loginIp);
			
			authDao.updateUserInfo(user);
		}
		

		
		
//		HttpSession session = request.getSession();
//
//		String loginSuccessMsg = "登录成功";
//		
//		String referer = request.getHeader("Referer");
//		
//		if(StringUtils.isNotBlank(referer)){
//			String path = request.getContextPath();
//			String basePath = new StringBuffer(request.getScheme()).append("://").append(request.getServerName()).append(":").append(request.getServerPort()).append(path).toString();
//			
//			if(!referer.startsWith(basePath)){
//				loginSuccessMsg = "登录成功,来源: " + referer;
//			}
//		}
//		
		super.onLoginSuccess(user, request, response);
		
		//在登录设置完成会话信息之后调用才能拿到用户信息。
		//LogService.log(OpType.login, loginSuccessMsg);
		
	}
	
	
	@Override
	public void onLogoutSuccess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
//		HttpSession session = request.getSession();

		// 清除菜单信息
//		session.removeAttribute(Constant.AUTH_SESS_MENU);
		
		//在清除会话信息之前调用才能拿到用户信息。
		//LogService.log(OpType.logout, "注销成功");
		
		super.onLogoutSuccess(request, response);
		
	
	}
	
}
